from hypium import UiDriver
from .absDriver import AbstractScriptDriver
from .utils import TimeStamp
from pathlib import Path

class HDriver(AbstractScriptDriver):
    _instance: UiDriver = None
    _device_sn = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = UiDriver.connect(
                device_sn = cls._device_sn,
                connector="hdc"
            )
        return cls._instance

    @classmethod
    def initializer(cls, device_sn):
        cls._device_sn = device_sn
        
    @classmethod
    def getInstance(self):
        return self._instance
    
    @classmethod
    def getScriptDriver(cls) -> UiDriver:
        if cls._instance is None:
            cls._instance = super(HDriver, cls).__new__(cls)
            cls._instance.driver = UiDriver.connect(
                device_sn = cls._device_sn,
                connector="hdc"
            )
        return cls._instance.driver
    
    @classmethod
    def getDeviceSerial(cls):
        """Get device serial number
        
        Returns:
            str: Device serial number
        """
        return cls._device_sn
    
    @classmethod
    def takeScreenshot(cls, output_dir, prop_name):
        Path(output_dir).mkdir(parents=True, exist_ok=True)
        stamp = TimeStamp().getCurrentTimeStamp()
        screenshot_file = str(Path(output_dir, f"{prop_name}_{stamp}.jpeg"))
        cls._instance.driver.capture_screen(screenshot_file)